#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "book.h"

int main()
{
    BookSystem* booksys = (BookSystem*)calloc(1, sizeof(BookSystem));
    char target[MAX_NAME_LEN];
    char name[MAX_NAME_LEN];
    double price;
    int amount;

    for ( ; ; ) {
        printf("\n%s", CHOOSE_MESSAGE);

        int choice = 0;
        scanf("%d", &choice);
        switch (choice) {
        //1.增加图书信息
        case 1:
            printf("%s", INPUT_NAME);
            scanf("%s", name);
            printf("%s", INPUT_PRICE);
            scanf("%lf", &price);
            printf("%s", INPUT_AMOUNT);
            scanf("%d", &amount);

            if (add_book(booksys, name, price, amount)) {
                puts("Add completed!");
            } else {
                puts("Add failed!");
            }
            break;
        //2.删除图书信息
        case 2:
            printf("%s", INPUT_NAME);
            scanf("%s", name);
            if (del_book(booksys, name)) {
                puts("delete completed!");
            } else {
                puts("delete failed!");
            }
            break;
        //3.打印所有图书信息
        case 3:
            showall(booksys);
            break;

        //4.更改图书信息
        case 4:
            printf("%s", INPUT_TARGET_BOOK);
            scanf("%s", target);

            printf("%s", INPUT_NAME);
            scanf("%s", name);
            printf("%s", INPUT_PRICE);
            scanf("%lf", &price);
            printf("%s", INPUT_AMOUNT);
            scanf("%d", &amount);

            if (change_book(booksys, target, name, price,amount)) {
                puts("Change completed!");
            } else {
                puts("Change failed!");
            }
            break;
        //5.序列化图书信息
        case 5:
            if (serialize_booksys(booksys)) {
                puts("Save completed!");
            } else {
                puts("Save failed!");
            }
            break;
        //6.反序列化图书信息
        case 6:
            booksys = unserialize_booksys("book.data");
            if (booksys == NULL) {
                puts("Load failed!");
            } else {
                puts("Load completed!");
            }
            break;
        //7.退出
        case 7:
            destory_booksys(booksys);
            exit(0);
        default:
            break;
        }
    }

    destory_booksys(booksys);
    return 0;
}